home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / xpcom / nsUnitConversion.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  7KB  |  215 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37. #ifndef nsUnitConversion_h__
  38. #define nsUnitConversion_h__
  39.  
  40. #include "nscore.h"
  41. #include "nsCoord.h"
  42. #include <math.h>
  43. #include <float.h>
  44.  
  45. #ifndef FLT_EPSILON
  46. // Not an ANSI compiler... oh, well.  Use an IEEE value.
  47. #define FLT_EPSILON 1.19209290e-7f
  48. #endif
  49. /// handy constants
  50. #define TWIPS_PER_POINT_INT           20
  51. #define TWIPS_PER_POINT_FLOAT         20.0f
  52. #define ROUND_CONST_FLOAT             0.5f
  53. #define CEIL_CONST_FLOAT              (1.0f - 0.5f*FLT_EPSILON)
  54.  
  55.  
  56. /*
  57.  * Coord Rounding Functions
  58.  */
  59. inline nscoord NSToCoordFloor(float aValue)
  60. {
  61. #ifdef NS_COORD_IS_FLOAT
  62.   return floorf(aValue);
  63. #else
  64.   return ((0.0f <= aValue) ? nscoord(aValue) : nscoord(aValue - CEIL_CONST_FLOAT));
  65. #endif
  66. }
  67.  
  68. inline nscoord NSToCoordCeil(float aValue)
  69. {
  70. #ifdef NS_COORD_IS_FLOAT
  71.   return ceilf(aValue);
  72. #else
  73.   return ((0.0f <= aValue) ? nscoord(aValue + CEIL_CONST_FLOAT) : nscoord(aValue));
  74. #endif
  75. }
  76.  
  77. inline nscoord NSToCoordRound(float aValue)
  78. {
  79. #ifdef NS_COORD_IS_FLOAT
  80.   return floorf(aValue + ROUND_CONST_FLOAT);
  81. #else
  82.   return ((0.0f <= aValue) ? nscoord(aValue + ROUND_CONST_FLOAT) : nscoord(aValue - ROUND_CONST_FLOAT));
  83. #endif
  84. }
  85.  
  86. /*
  87.  * Int Rounding Functions
  88.  */
  89. inline PRInt32 NSToIntFloor(float aValue)
  90. {
  91.   return ((0.0f <= aValue) ? PRInt32(aValue) : PRInt32(aValue - CEIL_CONST_FLOAT));
  92. }
  93.  
  94. inline PRInt32 NSToIntCeil(float aValue)
  95. {
  96.   return ((0.0f <= aValue) ? PRInt32(aValue + CEIL_CONST_FLOAT) : PRInt32(aValue));
  97. }
  98.  
  99. inline PRInt32 NSToIntRound(float aValue)
  100. {
  101.   return ((0.0f <= aValue) ? PRInt32(aValue + ROUND_CONST_FLOAT) : PRInt32(aValue - ROUND_CONST_FLOAT));
  102. }
  103.  
  104. /* 
  105.  * Twips/Points conversions
  106.  */
  107. inline nscoord NSFloatPointsToTwips(float aPoints)
  108. {
  109.   return NSToCoordRound(aPoints * TWIPS_PER_POINT_FLOAT);
  110. }
  111.  
  112. inline nscoord NSIntPointsToTwips(PRInt32 aPoints)
  113. {
  114.   // If nscoord is a float, do the multiplication as float to avoid
  115.   // overflow
  116.   return nscoord(aPoints) * TWIPS_PER_POINT_INT;
  117. }
  118.  
  119. inline PRInt32 NSTwipsToIntPoints(nscoord aTwips)
  120. {
  121.   return NSToIntRound(aTwips / TWIPS_PER_POINT_FLOAT);
  122. }
  123.  
  124. inline PRInt32 NSTwipsToFloorIntPoints(nscoord aTwips)
  125. {
  126.   return NSToIntFloor(aTwips / TWIPS_PER_POINT_FLOAT);
  127. }
  128.  
  129. inline PRInt32 NSTwipsToCeilIntPoints(nscoord aTwips)
  130. {
  131.   return NSToIntCeil(aTwips / TWIPS_PER_POINT_FLOAT);
  132. }
  133.  
  134. inline float NSTwipsToFloatPoints(nscoord aTwips)
  135. {
  136.   return (float(aTwips) / TWIPS_PER_POINT_FLOAT);
  137. }
  138.  
  139. /* 
  140.  * Twips/Pixel conversions
  141.  */
  142. inline nscoord NSFloatPixelsToTwips(float aPixels, float aTwipsPerPixel)
  143. {
  144.   nscoord r = NSToCoordRound(aPixels * aTwipsPerPixel);
  145.   VERIFY_COORD(r);
  146.   return r;
  147. }
  148.  
  149. inline nscoord NSIntPixelsToTwips(PRInt32 aPixels, float aTwipsPerPixel)
  150. {
  151. #ifdef NS_COORD_IS_FLOAT
  152.   nscoord r = aPixels * aTwipsPerPixel;
  153. #else
  154.   nscoord r = NSToCoordRound(float(aPixels) * aTwipsPerPixel);
  155. #endif
  156.   VERIFY_COORD(r);
  157.   return r;
  158. }
  159.  
  160. inline float NSTwipsToFloatPixels(nscoord aTwips, float aPixelsPerTwip)
  161. {
  162.   return (float(aTwips) * aPixelsPerTwip);
  163. }
  164.  
  165. inline PRInt32 NSTwipsToIntPixels(nscoord aTwips, float aPixelsPerTwip)
  166. {
  167.   return NSToIntRound(float(aTwips) * aPixelsPerTwip);
  168. }
  169.  
  170. /* 
  171.  * Twips/unit conversions
  172.  */
  173. inline nscoord NSUnitsToTwips(float aValue, float aPointsPerUnit)
  174. {
  175.   return NSToCoordRound(aValue * aPointsPerUnit * TWIPS_PER_POINT_FLOAT);
  176. }
  177.  
  178. inline float NSTwipsToUnits(nscoord aTwips, float aUnitsPerPoint)
  179. {
  180.   return (aTwips * (aUnitsPerPoint / TWIPS_PER_POINT_FLOAT));
  181. }
  182.  
  183.  
  184. /// Unit conversion macros
  185. //@{
  186. #define NS_INCHES_TO_TWIPS(x)         NSUnitsToTwips((x), 72.0f)                      // 72 points per inch
  187. #define NS_FEET_TO_TWIPS(x)           NSUnitsToTwips((x), (72.0f * 12.0f))            // 12 inches per foot
  188. #define NS_MILES_TO_TWIPS(x)          NSUnitsToTwips((x), (72.0f * 12.0f * 5280.0f))  // 5280 feet per mile
  189.  
  190. #define NS_MILLIMETERS_TO_TWIPS(x)    NSUnitsToTwips((x), (72.0f * 0.03937f))
  191. #define NS_CENTIMETERS_TO_TWIPS(x)    NSUnitsToTwips((x), (72.0f * 0.3937f))
  192. #define NS_METERS_TO_TWIPS(x)         NSUnitsToTwips((x), (72.0f * 39.37f))
  193. #define NS_KILOMETERS_TO_TWIPS(x)     NSUnitsToTwips((x), (72.0f * 39370.0f))
  194.  
  195. #define NS_PICAS_TO_TWIPS(x)          NSUnitsToTwips((x), 12.0f)                      // 12 points per pica
  196. #define NS_DIDOTS_TO_TWIPS(x)         NSUnitsToTwips((x), (16.0f / 15.0f))            // 15 didots per 16 points
  197. #define NS_CICEROS_TO_TWIPS(x)        NSUnitsToTwips((x), (12.0f * (16.0f / 15.0f)))  // 12 didots per cicero
  198.  
  199.  
  200. #define NS_TWIPS_TO_INCHES(x)         NSTwipsToUnits((x), 1.0f / 72.0f)
  201. #define NS_TWIPS_TO_FEET(x)           NSTwipsToUnits((x), 1.0f / (72.0f * 12.0f))
  202. #define NS_TWIPS_TO_MILES(x)          NSTwipsToUnits((x), 1.0f / (72.0f * 12.0f * 5280.0f))
  203.  
  204. #define NS_TWIPS_TO_MILLIMETERS(x)    NSTwipsToUnits((x), 1.0f / (72.0f * 0.03937f))
  205. #define NS_TWIPS_TO_CENTIMETERS(x)    NSTwipsToUnits((x), 1.0f / (72.0f * 0.3937f))
  206. #define NS_TWIPS_TO_METERS(x)         NSTwipsToUnits((x), 1.0f / (72.0f * 39.37f))
  207. #define NS_TWIPS_TO_KILOMETERS(x)     NSTwipsToUnits((x), 1.0f / (72.0f * 39370.0f))
  208.  
  209. #define NS_TWIPS_TO_PICAS(x)          NSTwipsToUnits((x), 1.0f / 12.0f)
  210. #define NS_TWIPS_TO_DIDOTS(x)         NSTwipsToUnits((x), 1.0f / (16.0f / 15.0f))
  211. #define NS_TWIPS_TO_CICEROS(x)        NSTwipsToUnits((x), 1.0f / (12.0f * (16.0f / 15.0f)))
  212. //@}
  213.  
  214. #endif
  215.